package com.zHan.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zHan.sky.common.contant.MessageConstant;
import com.zHan.sky.common.contant.StatusConstant;
import com.zHan.sky.common.content.BaseContext;
import com.zHan.sky.common.exception.AccountLockedException;
import com.zHan.sky.common.exception.AccountNotFoundException;
import com.zHan.sky.common.exception.PasswordEditFailedException;
import com.zHan.sky.common.exception.PasswordErrorException;
import com.zHan.sky.dto.EmployeeLoginDTO;
import com.zHan.sky.dto.PasswordEditDTO;
import com.zHan.sky.entity.Employee;
import com.zHan.sky.mapper.EmployeeMapper;
import com.zHan.sky.service.EmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    /**
     * 用户登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = lambdaQuery().eq(Employee::getUsername, username).one();

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 密码比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 修改密码
     *
     * @param passwordEditDTO
     */
    @Override
    public void updatePassword(PasswordEditDTO passwordEditDTO) {
        // 对新旧密码md5加密
        String oldPassword =
                DigestUtils.md5DigestAsHex(passwordEditDTO.getOldPassword().getBytes());
        String newPassword =
                DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes());
        // 如果新旧密码为空，抛出业务异常
        if (!StringUtils.hasText(oldPassword) || !StringUtils.hasText(newPassword)) {
            throw new PasswordEditFailedException(MessageConstant.PASSWORD_EDIT_FAILED);
        }
        // 查询员工
        Employee employee = lambdaQuery()
                .eq(Employee::getId, BaseContext.getCurrentId())
                .eq(Employee::getPassword, oldPassword)
                .one();
        // 如果员工为空，旧密码错误
        if (employee == null) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        // 判断输入的新密码是否和数据库的旧密码一样
        if (newPassword.equals(employee.getPassword())) {
            // 旧密码为空、新密码为空、新密码和旧密码一样，抛出业务异常
            throw new PasswordEditFailedException(MessageConstant.PASSWORD_EDIT_FAILED);
        }
        // 修改密码
        employee.setPassword(newPassword);
        updateById(employee);
    }
}
